import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;

public class LabelParser {

	public LabelParser() {
		BufferedReader in;
		ObjectOutputStream out;
		String line = null;

		try {
			// ucitavanje labela tabela
			
			HashMap<String, String> tableLabels = new HashMap<String, String>();
			in = new BufferedReader(new InputStreamReader(new FileInputStream("tabele.txt"), "UTF-8"));

			while ((line = in.readLine()) != null) {
				if (line.startsWith("name")) {
					String name = line.substring(line.indexOf(":") + 1, line.indexOf(","));
					line = line.substring(line.indexOf(",") + 1);
					String code = line.substring(line.indexOf(":") + 1, line.indexOf(","));
					tableLabels.put(code, name);
				}
			}
			
			in.close();
			
			out = new ObjectOutputStream(new FileOutputStream("table_labels.eb"));
			out.writeObject(tableLabels);
			out.close();
			
			// ucitavanje labela kolona
			
			HashMap<String, HashMap<String, String>> columnLabels = new HashMap<String, HashMap<String, String>>();
			in = new BufferedReader(new InputStreamReader(new FileInputStream("kolone.txt"), "UTF-8"));
			
			while ((line = in.readLine()) != null) {
				if (line.startsWith("tname")) {
					String tcode = line.substring(line.lastIndexOf(":")+1, line.lastIndexOf(","));
					HashMap<String, String> col = new HashMap<String, String>();
					while (!(line = in.readLine()).equals("#")) {
						String cname = line.substring(line.indexOf(":") + 1, line.indexOf(","));
						line = line.substring(line.indexOf(",") + 1);
						String ccode = line.substring(line.indexOf(":") + 1, line.indexOf(","));
						//System.out.println(tcode+" // "+ccode+" : "+cname);
						col.put(ccode, cname);
					}
					columnLabels.put(tcode, col);
				}
			}
			
			in.close();
			
			out = new ObjectOutputStream(new FileOutputStream("column_labels.eb"));
			out.writeObject(columnLabels);
			out.close();
		}
		catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		new LabelParser();
	}
}